
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>类型转换 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="19_Nested_Types.html" />
    
    
    <link rel="prev" href="17_Error_Handling.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01_The_Basics.html">
            
                <a href="01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02_Basic_Operators.html">
            
                <a href="02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03_Strings_and_Characters.html">
            
                <a href="03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04_Collection_Types.html">
            
                <a href="04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="05_Control_Flow.html">
            
                <a href="05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="06_Functions.html">
            
                <a href="06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="07_Closures.html">
            
                <a href="07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="08_Enumerations.html">
            
                <a href="08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="09_Structures_And_Classes.html">
            
                <a href="09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="10_Properties.html">
            
                <a href="10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="11_Methods.html">
            
                <a href="11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="12_Subscripts.html">
            
                <a href="12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="13_Inheritance.html">
            
                <a href="13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="14_Initialization.html">
            
                <a href="14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="15_Deinitialization.html">
            
                <a href="15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="16_Optional_Chaining.html">
            
                <a href="16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="17_Error_Handling.html">
            
                <a href="17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.18" data-path="18_Type_Casting.html">
            
                <a href="18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="19_Nested_Types.html">
            
                <a href="19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="20_Extensions.html">
            
                <a href="20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="21_Protocols.html">
            
                <a href="21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="22_Generics.html">
            
                <a href="22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="23_Automatic_Reference_Counting.html">
            
                <a href="23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="24_Memory_Safety.html">
            
                <a href="24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="25_Access_Control.html">
            
                <a href="25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="26_Advanced_Operators.html">
            
                <a href="26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >类型转换</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7C7B;&#x578B;&#x8F6C;&#x6362;">&#x7C7B;&#x578B;&#x8F6C;&#x6362;</h1>
<p><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;</em>&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x5B9E;&#x4F8B;&#x770B;&#x505A;&#x662F;&#x5176;&#x7236;&#x7C7B;&#x6216;&#x8005;&#x5B50;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x5728; Swift &#x4E2D;&#x4F7F;&#x7528; <code>is</code> &#x548C; <code>as</code> &#x64CD;&#x4F5C;&#x7B26;&#x5B9E;&#x73B0;&#x3002;&#x8FD9;&#x4E24;&#x4E2A;&#x64CD;&#x4F5C;&#x7B26;&#x5206;&#x522B;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x7B80;&#x5355;&#x8FBE;&#x610F;&#x7684;&#x65B9;&#x5F0F;&#x53BB;&#x68C0;&#x67E5;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x6216;&#x8005;&#x8F6C;&#x6362;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x5B83;&#x6765;&#x68C0;&#x67E5;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x9075;&#x5FAA;&#x4E86;&#x67D0;&#x4E2A;&#x534F;&#x8BAE;&#xFF0C;&#x5C31;&#x50CF;&#x5728; <a href="21_Protocols.html#checking_for_protocol_conformance">&#x68C0;&#x9A8C;&#x534F;&#x8BAE;&#x9075;&#x5FAA;</a> &#x90E8;&#x5206;&#x8BB2;&#x8FF0;&#x7684;&#x4E00;&#x6837;&#x3002;</p>
<h2 id="defining-a-class-hierarchy-for-type-casting">&#x4E3A;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x5B9A;&#x4E49;&#x7C7B;&#x5C42;&#x6B21; </h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x7528;&#x5728;&#x7C7B;&#x548C;&#x5B50;&#x7C7B;&#x7684;&#x5C42;&#x6B21;&#x7ED3;&#x6784;&#x4E0A;&#xFF0C;&#x68C0;&#x67E5;&#x7279;&#x5B9A;&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#x5E76;&#x4E14;&#x8F6C;&#x6362;&#x8FD9;&#x4E2A;&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#x6210;&#x4E3A;&#x8FD9;&#x4E2A;&#x5C42;&#x6B21;&#x7ED3;&#x6784;&#x4E2D;&#x7684;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4E09;&#x4E2A;&#x4EE3;&#x7801;&#x6BB5;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x7C7B;&#x5C42;&#x6B21;&#x548C;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x4E86;&#x8FD9;&#x4E9B;&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x4F5C;&#x4E3A;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<p>&#x7B2C;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x7247;&#x6BB5;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x57FA;&#x7C7B; <code>MediaItem</code>&#x3002;&#x8FD9;&#x4E2A;&#x7C7B;&#x4E3A;&#x4EFB;&#x4F55;&#x51FA;&#x73B0;&#x5728;&#x6570;&#x5B57;&#x5A92;&#x4F53;&#x5E93;&#x7684;&#x5A92;&#x4F53;&#x9879;&#x63D0;&#x4F9B;&#x57FA;&#x7840;&#x529F;&#x80FD;&#x3002;&#x7279;&#x522B;&#x7684;&#xFF0C;&#x5B83;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A; <code>String</code> &#x7C7B;&#x578B;&#x7684; <code>name</code> &#x5C5E;&#x6027;&#xFF0C;&#x548C;&#x4E00;&#x4E2A; <code>init(name:)</code> &#x521D;&#x59CB;&#x5316;&#x5668;&#x3002;&#xFF08;&#x5047;&#x5B9A;&#x6240;&#x6709;&#x7684;&#x5A92;&#x4F53;&#x9879;&#x90FD;&#x6709;&#x4E2A;&#x540D;&#x79F0;&#x3002;&#xFF09;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MediaItem</span> </span>{
    <span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span>
    <span class="hljs-keyword">init</span>(name: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.name = name
    }
}
</code></pre>
<p>&#x4E0B;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x6BB5;&#x5B9A;&#x4E49;&#x4E86; <code>MediaItem</code> &#x7684;&#x4E24;&#x4E2A;&#x5B50;&#x7C7B;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B; <code>Movie</code> &#x5C01;&#x88C5;&#x4E86;&#x4E0E;&#x7535;&#x5F71;&#x76F8;&#x5173;&#x7684;&#x989D;&#x5916;&#x4FE1;&#x606F;&#xFF0C;&#x5728;&#x7236;&#x7C7B;&#xFF08;&#x6216;&#x8005;&#x8BF4;&#x57FA;&#x7C7B;&#xFF09;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x4E2A; <code>director</code>&#xFF08;&#x5BFC;&#x6F14;&#xFF09;&#x5C5E;&#x6027;&#xFF0C;&#x548C;&#x76F8;&#x5E94;&#x7684;&#x521D;&#x59CB;&#x5316;&#x5668;&#x3002;&#x7B2C;&#x4E8C;&#x4E2A;&#x5B50;&#x7C7B; <code>Song</code>&#xFF0C;&#x5728;&#x7236;&#x7C7B;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x4E2A; <code>artist</code>&#xFF08;&#x827A;&#x672F;&#x5BB6;&#xFF09;&#x5C5E;&#x6027;&#xFF0C;&#x548C;&#x76F8;&#x5E94;&#x7684;&#x521D;&#x59CB;&#x5316;&#x5668;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Movie</span>: <span class="hljs-title">MediaItem</span> </span>{
    <span class="hljs-keyword">var</span> director: <span class="hljs-type">String</span>
    <span class="hljs-keyword">init</span>(name: <span class="hljs-type">String</span>, director: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.director = director
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(name: name)
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Song</span>: <span class="hljs-title">MediaItem</span> </span>{
    <span class="hljs-keyword">var</span> artist: <span class="hljs-type">String</span>
    <span class="hljs-keyword">init</span>(name: <span class="hljs-type">String</span>, artist: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.artist = artist
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(name: name)
    }
}
</code></pre>
<p>&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x6BB5;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x5E38;&#x91CF; <code>library</code>&#xFF0C;&#x5305;&#x542B;&#x4E24;&#x4E2A; <code>Movie</code> &#x5B9E;&#x4F8B;&#x548C;&#x4E09;&#x4E2A; <code>Song</code> &#x5B9E;&#x4F8B;&#x3002;<code>library</code> &#x7684;&#x7C7B;&#x578B;&#x662F;&#x5728;&#x5B83;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x6839;&#x636E;&#x5B83;&#x6570;&#x7EC4;&#x4E2D;&#x6240;&#x5305;&#x542B;&#x7684;&#x5185;&#x5BB9;&#x63A8;&#x65AD;&#x6765;&#x7684;&#x3002;Swift &#x7684;&#x7C7B;&#x578B;&#x68C0;&#x6D4B;&#x5668;&#x80FD;&#x591F;&#x63A8;&#x65AD;&#x51FA; <code>Movie</code> &#x548C; <code>Song</code> &#x6709;&#x5171;&#x540C;&#x7684;&#x7236;&#x7C7B; <code>MediaItem</code>&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x63A8;&#x65AD;&#x51FA; <code>[MediaItem]</code> &#x7C7B;&#x4F5C;&#x4E3A; <code>library</code> &#x7684;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> library = [
    <span class="hljs-type">Movie</span>(name: <span class="hljs-string">&quot;Casablanca&quot;</span>, director: <span class="hljs-string">&quot;Michael Curtiz&quot;</span>),
    <span class="hljs-type">Song</span>(name: <span class="hljs-string">&quot;Blue Suede Shoes&quot;</span>, artist: <span class="hljs-string">&quot;Elvis Presley&quot;</span>),
    <span class="hljs-type">Movie</span>(name: <span class="hljs-string">&quot;Citizen Kane&quot;</span>, director: <span class="hljs-string">&quot;Orson Welles&quot;</span>),
    <span class="hljs-type">Song</span>(name: <span class="hljs-string">&quot;The One And Only&quot;</span>, artist: <span class="hljs-string">&quot;Chesney Hawkes&quot;</span>),
    <span class="hljs-type">Song</span>(name: <span class="hljs-string">&quot;Never Gonna Give You Up&quot;</span>, artist: <span class="hljs-string">&quot;Rick Astley&quot;</span>)
]
<span class="hljs-comment">// &#x6570;&#x7EC4; library &#x7684;&#x7C7B;&#x578B;&#x88AB;&#x63A8;&#x65AD;&#x4E3A; [MediaItem]</span>
</code></pre>
<p>&#x5728;&#x5E55;&#x540E; <code>library</code> &#x91CC;&#x5B58;&#x50A8;&#x7684;&#x5A92;&#x4F53;&#x9879;&#x4F9D;&#x7136;&#x662F; <code>Movie</code> &#x548C; <code>Song</code> &#x7C7B;&#x578B;&#x7684;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x82E5;&#x4F60;&#x8FED;&#x4EE3;&#x5B83;&#xFF0C;&#x4F9D;&#x6B21;&#x53D6;&#x51FA;&#x7684;&#x5B9E;&#x4F8B;&#x4F1A;&#x662F; <code>MediaItem</code> &#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x800C;&#x4E0D;&#x662F; <code>Movie</code> &#x548C; <code>Song</code> &#x7C7B;&#x578B;&#x3002;&#x4E3A;&#x4E86;&#x8BA9;&#x5B83;&#x4EEC;&#x4F5C;&#x4E3A;&#x539F;&#x672C;&#x7684;&#x7C7B;&#x578B;&#x5DE5;&#x4F5C;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x68C0;&#x67E5;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x6216;&#x8005;&#x5411;&#x4E0B;&#x8F6C;&#x6362;&#x5B83;&#x4EEC;&#x5230;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x63CF;&#x8FF0;&#x7684;&#x4E00;&#x6837;&#x3002;</p>
<h2 id="checking-type">&#x68C0;&#x67E5;&#x7C7B;&#x578B; </h2>
<p>&#x7528;<em>&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x64CD;&#x4F5C;&#x7B26;</em>&#xFF08;<code>is</code>&#xFF09;&#x6765;&#x68C0;&#x67E5;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x662F;&#x5426;&#x5C5E;&#x4E8E;&#x7279;&#x5B9A;&#x5B50;&#x7C7B;&#x578B;&#x3002;&#x82E5;&#x5B9E;&#x4F8B;&#x5C5E;&#x4E8E;&#x90A3;&#x4E2A;&#x5B50;&#x7C7B;&#x578B;&#xFF0C;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x64CD;&#x4F5C;&#x7B26;&#x8FD4;&#x56DE; <code>true</code>&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; <code>false</code>&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;<code>movieCount</code> &#x548C; <code>songCount</code>&#xFF0C;&#x7528;&#x6765;&#x8BA1;&#x7B97;&#x6570;&#x7EC4; <code>library</code> &#x4E2D; <code>Movie</code> &#x548C; <code>Song</code> &#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x6570;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> movieCount = <span class="hljs-number">0</span>
<span class="hljs-keyword">var</span> songCount = <span class="hljs-number">0</span>

<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> library {
    <span class="hljs-keyword">if</span> item <span class="hljs-keyword">is</span> <span class="hljs-type">Movie</span> {
        movieCount += <span class="hljs-number">1</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> item <span class="hljs-keyword">is</span> <span class="hljs-type">Song</span> {
        songCount += <span class="hljs-number">1</span>
    }
}

<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Media library contains <span class="hljs-subst">\(movieCount)</span> movies and <span class="hljs-subst">\(songCount)</span> songs&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Media library contains 2 movies and 3 songs&#x201D;</span>
</code></pre>
<p>&#x793A;&#x4F8B;&#x8FED;&#x4EE3;&#x4E86;&#x6570;&#x7EC4; <code>library</code> &#x4E2D;&#x7684;&#x6240;&#x6709;&#x9879;&#x3002;&#x6BCF;&#x4E00;&#x6B21;&#xFF0C;<code>for-in</code> &#x5FAA;&#x73AF;&#x8BBE;&#x7F6E;
<code>item</code> &#x5E38;&#x91CF;&#x4E3A;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x4E0B;&#x4E00;&#x4E2A; <code>MediaItem</code> &#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x82E5;&#x5F53;&#x524D; <code>MediaItem</code> &#x662F;&#x4E00;&#x4E2A; <code>Movie</code> &#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;<code>item is Movie</code> &#x8FD4;&#x56DE;
<code>true</code>&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; <code>false</code>&#x3002;&#x540C;&#x6837;&#x7684;&#xFF0C;<code>item is Song</code> &#x68C0;&#x67E5; <code>item</code> &#x662F;&#x5426;&#x4E3A; <code>Song</code> &#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x5728;&#x5FAA;&#x73AF;&#x7ED3;&#x675F;&#x540E;&#xFF0C;<code>movieCount</code> &#x548C; <code>songCount</code> &#x7684;&#x503C;&#x5C31;&#x662F;&#x88AB;&#x627E;&#x5230;&#x7684;&#x5C5E;&#x4E8E;&#x5404;&#x81EA;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x7684;&#x6570;&#x91CF;&#x3002;</p>
<h2 id="downcasting">&#x5411;&#x4E0B;&#x8F6C;&#x578B; </h2>
<p>&#x67D0;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x6216;&#x53D8;&#x91CF;&#x53EF;&#x80FD;&#x5728;&#x5E55;&#x540E;&#x5B9E;&#x9645;&#x4E0A;&#x5C5E;&#x4E8E;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B;&#x3002;&#x5F53;&#x786E;&#x5B9A;&#x662F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5C1D;&#x8BD5;&#x7528;<em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x7B26;</em>&#xFF08;<code>as?</code> &#x6216; <code>as!</code>&#xFF09;&#x5411;&#x4E0B;&#x8F6C;&#x5230;&#x5B83;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x56E0;&#x4E3A;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x53EF;&#x80FD;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x7C7B;&#x578B;&#x8F6C;&#x578B;&#x64CD;&#x4F5C;&#x7B26;&#x5E26;&#x6709;&#x4E24;&#x79CD;&#x4E0D;&#x540C;&#x5F62;&#x5F0F;&#x3002;&#x6761;&#x4EF6;&#x5F62;&#x5F0F; <code>as?</code> &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x4F60;&#x8BD5;&#x56FE;&#x5411;&#x4E0B;&#x8F6C;&#x6210;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x53EF;&#x9009;&#x503C;&#x3002;&#x5F3A;&#x5236;&#x5F62;&#x5F0F; <code>as!</code> &#x628A;&#x8BD5;&#x56FE;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x548C;&#x5F3A;&#x5236;&#x89E3;&#x5305;&#x8F6C;&#x6362;&#x7ED3;&#x679C;&#x7ED3;&#x5408;&#x4E3A;&#x4E00;&#x4E2A;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x5F53;&#x4F60;&#x4E0D;&#x786E;&#x5B9A;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x53EF;&#x4EE5;&#x6210;&#x529F;&#x65F6;&#xFF0C;&#x7528;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x7684;&#x6761;&#x4EF6;&#x5F62;&#x5F0F;&#xFF08;<code>as?</code>&#xFF09;&#x3002;&#x6761;&#x4EF6;&#x5F62;&#x5F0F;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x603B;&#x662F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x82E5;&#x4E0B;&#x8F6C;&#x662F;&#x4E0D;&#x53EF;&#x80FD;&#x7684;&#xFF0C;&#x53EF;&#x9009;&#x503C;&#x5C06;&#x662F; <code>nil</code>&#x3002;&#x8FD9;&#x4F7F;&#x4F60;&#x80FD;&#x591F;&#x68C0;&#x67E5;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;</p>
<p>&#x53EA;&#x6709;&#x4F60;&#x53EF;&#x4EE5;&#x786E;&#x5B9A;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x4E00;&#x5B9A;&#x4F1A;&#x6210;&#x529F;&#x65F6;&#xFF0C;&#x624D;&#x4F7F;&#x7528;&#x5F3A;&#x5236;&#x5F62;&#x5F0F;&#xFF08;<code>as!</code>&#xFF09;&#x3002;&#x5F53;&#x4F60;&#x8BD5;&#x56FE;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x4E3A;&#x4E00;&#x4E2A;&#x4E0D;&#x6B63;&#x786E;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5F3A;&#x5236;&#x5F62;&#x5F0F;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4F1A;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x8FED;&#x4EE3;&#x4E86; <code>library</code> &#x91CC;&#x7684;&#x6BCF;&#x4E00;&#x4E2A; <code>MediaItem</code>&#xFF0C;&#x5E76;&#x6253;&#x5370;&#x51FA;&#x9002;&#x5F53;&#x7684;&#x63CF;&#x8FF0;&#x3002;&#x8981;&#x8FD9;&#x6837;&#x505A;&#xFF0C;<code>item</code> &#x9700;&#x8981;&#x771F;&#x6B63;&#x4F5C;&#x4E3A; <code>Movie</code> &#x6216; <code>Song</code> &#x7684;&#x7C7B;&#x578B;&#x6765;&#x4F7F;&#x7528;&#xFF0C;&#x800C;&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x4F5C;&#x4E3A; <code>MediaItem</code>&#x3002;&#x4E3A;&#x4E86;&#x80FD;&#x591F;&#x5728;&#x63CF;&#x8FF0;&#x4E2D;&#x4F7F;&#x7528; <code>Movie</code> &#x6216; <code>Song</code> &#x7684; <code>director</code> &#x6216; <code>artist</code> &#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x662F;&#x5FC5;&#x8981;&#x7684;&#x3002;</p>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x4E2A; <code>item</code> &#x53EF;&#x80FD;&#x662F; <code>Movie</code> &#x6216; <code>Song</code>&#x3002;&#x4E8B;&#x524D;&#x4F60;&#x4E0D;&#x77E5;&#x9053;&#x6BCF;&#x4E2A; <code>item</code> &#x7684;&#x771F;&#x5B9E;&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x6761;&#x4EF6;&#x5F62;&#x5F0F;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF08;<code>as?</code>&#xFF09;&#x53BB;&#x68C0;&#x67E5;&#x5FAA;&#x73AF;&#x91CC;&#x7684;&#x6BCF;&#x6B21;&#x4E0B;&#x8F6C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> library {
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> movie = item <span class="hljs-keyword">as</span>? <span class="hljs-type">Movie</span> {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Movie: <span class="hljs-subst">\(movie.name)</span>, dir. <span class="hljs-subst">\(movie.director)</span>&quot;</span>)
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> song = item <span class="hljs-keyword">as</span>? <span class="hljs-type">Song</span> {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Song: <span class="hljs-subst">\(song.name)</span>, by <span class="hljs-subst">\(song.artist)</span>&quot;</span>)
    }
}

<span class="hljs-comment">// Movie: Casablanca, dir. Michael Curtiz</span>
<span class="hljs-comment">// Song: Blue Suede Shoes, by Elvis Presley</span>
<span class="hljs-comment">// Movie: Citizen Kane, dir. Orson Welles</span>
<span class="hljs-comment">// Song: The One And Only, by Chesney Hawkes</span>
<span class="hljs-comment">// Song: Never Gonna Give You Up, by Rick Astley</span>
</code></pre>
<p>&#x793A;&#x4F8B;&#x9996;&#x5148;&#x8BD5;&#x56FE;&#x5C06; <code>item</code> &#x4E0B;&#x8F6C;&#x4E3A; <code>Movie</code>&#x3002;&#x56E0;&#x4E3A; <code>item</code> &#x662F;&#x4E00;&#x4E2A; <code>MediaItem</code>
&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x5B83;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A; <code>Movie</code>&#xFF1B;&#x540C;&#x6837;&#xFF0C;&#x5B83;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A; <code>Song</code>&#xFF0C;&#x6216;&#x8005;&#x4EC5;&#x4EC5;&#x662F;&#x57FA;&#x7C7B;
<code>MediaItem</code>&#x3002;&#x56E0;&#x4E3A;&#x4E0D;&#x786E;&#x5B9A;&#xFF0C;<code>as?</code> &#x5F62;&#x5F0F;&#x5728;&#x8BD5;&#x56FE;&#x4E0B;&#x8F6C;&#x65F6;&#x5C06;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x3002;<code>item as? Movie</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F; <code>Movie?</code> &#x6216;&#x8005;&#x8BF4;&#x201C;&#x53EF;&#x9009; <code>Movie</code>&#x201D;&#x3002;</p>
<p>&#x5F53;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x4E3A; <code>Movie</code> &#x5E94;&#x7528;&#x5728;&#x4E24;&#x4E2A; <code>Song</code>
&#x5B9E;&#x4F8B;&#x65F6;&#x5C06;&#x4F1A;&#x5931;&#x8D25;&#x3002;&#x4E3A;&#x4E86;&#x5904;&#x7406;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4F7F;&#x7528;&#x4E86;&#x53EF;&#x9009;&#x7ED1;&#x5B9A;&#xFF08;optional binding&#xFF09;&#x6765;&#x68C0;&#x67E5;&#x53EF;&#x9009; <code>Movie</code> &#x771F;&#x7684;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x503C;&#xFF08;&#x8FD9;&#x4E2A;&#x662F;&#x4E3A;&#x4E86;&#x5224;&#x65AD;&#x4E0B;&#x8F6C;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;&#xFF09;&#x53EF;&#x9009;&#x7ED1;&#x5B9A;&#x662F;&#x8FD9;&#x6837;&#x5199;&#x7684;&#x201C;<code>if let movie = item as? Movie</code>&#x201D;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x89E3;&#x8BFB;&#xFF1A;</p>
<p>&#x201C;&#x5C1D;&#x8BD5;&#x5C06; <code>item</code> &#x8F6C;&#x4E3A; <code>Movie</code> &#x7C7B;&#x578B;&#x3002;&#x82E5;&#x6210;&#x529F;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x4E34;&#x65F6;&#x5E38;&#x91CF; <code>movie</code> &#x6765;&#x5B58;&#x50A8;&#x8FD4;&#x56DE;&#x7684;&#x53EF;&#x9009; <code>Movie</code> &#x4E2D;&#x7684;&#x503C;&#x201D;</p>
<p>&#x82E5;&#x5411;&#x4E0B;&#x8F6C;&#x578B;&#x6210;&#x529F;&#xFF0C;&#x7136;&#x540E; <code>movie</code> &#x7684;&#x5C5E;&#x6027;&#x5C06;&#x7528;&#x4E8E;&#x6253;&#x5370;&#x4E00;&#x4E2A; <code>Movie</code> &#x5B9E;&#x4F8B;&#x7684;&#x63CF;&#x8FF0;&#xFF0C;&#x5305;&#x62EC;&#x5B83;&#x7684;&#x5BFC;&#x6F14;&#x7684;&#x540D;&#x5B57; <code>director</code>&#x3002;&#x76F8;&#x4F3C;&#x7684;&#x539F;&#x7406;&#x88AB;&#x7528;&#x6765;&#x68C0;&#x6D4B; <code>Song</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x5F53; <code>Song</code> &#x88AB;&#x627E;&#x5230;&#x65F6;&#x5219;&#x6253;&#x5370;&#x5B83;&#x7684;&#x63CF;&#x8FF0;&#xFF08;&#x5305;&#x542B; <code>artist</code> &#x7684;&#x540D;&#x5B57;&#xFF09;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x8F6C;&#x6362;&#x6CA1;&#x6709;&#x771F;&#x7684;&#x6539;&#x53D8;&#x5B9E;&#x4F8B;&#x6216;&#x5B83;&#x7684;&#x503C;&#x3002;&#x6839;&#x672C;&#x7684;&#x5B9E;&#x4F8B;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#xFF1B;&#x53EA;&#x662F;&#x7B80;&#x5355;&#x5730;&#x628A;&#x5B83;&#x4F5C;&#x4E3A;&#x5B83;&#x88AB;&#x8F6C;&#x6362;&#x6210;&#x7684;&#x7C7B;&#x578B;&#x6765;&#x4F7F;&#x7528;&#x3002;</p>
</blockquote>
<h2 id="type-casting-for-any-and-anyobject"><code>Any</code> &#x548C; <code>AnyObject</code> &#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362; </h2>
<p>Swift &#x4E3A;&#x4E0D;&#x786E;&#x5B9A;&#x7C7B;&#x578B;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x7C7B;&#x578B;&#x522B;&#x540D;&#xFF1A;</p>
<ul>
<li><code>Any</code> &#x53EF;&#x4EE5;&#x8868;&#x793A;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#xFF0C;&#x5305;&#x62EC;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x3002;</li>
<li><code>AnyObject</code> &#x53EF;&#x4EE5;&#x8868;&#x793A;&#x4EFB;&#x4F55;&#x7C7B;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</li>
</ul>
<p>&#x53EA;&#x6709;&#x5F53;&#x4F60;&#x786E;&#x5B9E;&#x9700;&#x8981;&#x5B83;&#x4EEC;&#x7684;&#x884C;&#x4E3A;&#x548C;&#x529F;&#x80FD;&#x65F6;&#x624D;&#x4F7F;&#x7528; <code>Any</code> &#x548C; <code>AnyObject</code>&#x3002;&#x6700;&#x597D;&#x8FD8;&#x662F;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x6307;&#x660E;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x6709;&#x4E2A;&#x793A;&#x4F8B;&#xFF0C;&#x4F7F;&#x7528; <code>Any</code> &#x7C7B;&#x578B;&#x6765;&#x548C;&#x6DF7;&#x5408;&#x7684;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x4E00;&#x8D77;&#x5DE5;&#x4F5C;&#xFF0C;&#x5305;&#x62EC;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x548C;&#x975E;&#x7C7B;&#x7C7B;&#x578B;&#x3002;&#x5B83;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x5B58;&#x50A8; <code>Any</code> &#x7C7B;&#x578B;&#x7684;&#x6570;&#x7EC4; <code>things</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> things = [<span class="hljs-type">Any</span>]()

things.append(<span class="hljs-number">0</span>)
things.append(<span class="hljs-number">0.0</span>)
things.append(<span class="hljs-number">42</span>)
things.append(<span class="hljs-number">3.14159</span>)
things.append(<span class="hljs-string">&quot;hello&quot;</span>)
things.append((<span class="hljs-number">3.0</span>, <span class="hljs-number">5.0</span>))
things.append(<span class="hljs-type">Movie</span>(name: <span class="hljs-string">&quot;Ghostbusters&quot;</span>, director: <span class="hljs-string">&quot;Ivan Reitman&quot;</span>))
things.append({ (name: <span class="hljs-type">String</span>) -&gt; <span class="hljs-type">String</span> <span class="hljs-keyword">in</span> <span class="hljs-string">&quot;Hello, <span class="hljs-subst">\(name)</span>&quot;</span> })
</code></pre>
<p><code>things</code> &#x6570;&#x7EC4;&#x5305;&#x542B;&#x4E24;&#x4E2A; <code>Int</code> &#x503C;&#xFF0C;&#x4E24;&#x4E2A; <code>Double</code> &#x503C;&#xFF0C;&#x4E00;&#x4E2A; <code>String</code> &#x503C;&#xFF0C;&#x4E00;&#x4E2A;&#x5143;&#x7EC4; <code>(Double, Double)</code>&#xFF0C;&#x4E00;&#x4E2A; <code>Movie</code> &#x5B9E;&#x4F8B;&#x201C;Ghostbusters&#x201D;&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x4E2A;&#x63A5;&#x53D7; <code>String</code> &#x503C;&#x5E76;&#x8FD4;&#x56DE;&#x53E6;&#x4E00;&#x4E2A; <code>String</code> &#x503C;&#x7684;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728; <code>switch</code> &#x8868;&#x8FBE;&#x5F0F;&#x7684; <code>case</code> &#x4E2D;&#x4F7F;&#x7528; <code>is</code> &#x548C; <code>as</code> &#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x627E;&#x51FA;&#x53EA;&#x77E5;&#x9053;&#x662F; <code>Any</code> &#x6216; <code>AnyObject</code> &#x7C7B;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x6216;&#x53D8;&#x91CF;&#x7684;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x8FED;&#x4EE3; <code>things</code> &#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x9879;&#xFF0C;&#x5E76;&#x7528; <code>switch</code> &#x8BED;&#x53E5;&#x67E5;&#x627E;&#x6BCF;&#x4E00;&#x9879;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x6709;&#x51E0;&#x4E2A; <code>switch</code> &#x8BED;&#x53E5;&#x7684; <code>case</code> &#x7ED1;&#x5B9A;&#x5B83;&#x4EEC;&#x5339;&#x914D;&#x5230;&#x7684;&#x503C;&#x5230;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x5E38;&#x91CF;&#xFF0C;&#x4ECE;&#x800C;&#x53EF;&#x4EE5;&#x6253;&#x5370;&#x8FD9;&#x4E9B;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">for</span> thing <span class="hljs-keyword">in</span> things {
    <span class="hljs-keyword">switch</span> thing {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> <span class="hljs-keyword">as</span> <span class="hljs-type">Int</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;zero as an Int&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> <span class="hljs-keyword">as</span> <span class="hljs-type">Double</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;zero as a Double&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> someInt <span class="hljs-keyword">as</span> <span class="hljs-type">Int</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;an integer value of <span class="hljs-subst">\(someInt)</span>&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> someDouble <span class="hljs-keyword">as</span> <span class="hljs-type">Double</span> <span class="hljs-keyword">where</span> someDouble &gt; <span class="hljs-number">0</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;a positive double value of <span class="hljs-subst">\(someDouble)</span>&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">is</span> <span class="hljs-type">Double</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;some other double value that I don&apos;t want to print&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> someString <span class="hljs-keyword">as</span> <span class="hljs-type">String</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;a string value of \&quot;<span class="hljs-subst">\(someString)</span>\&quot;&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> (x, y) <span class="hljs-keyword">as</span> (<span class="hljs-type">Double</span>, <span class="hljs-type">Double</span>):
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;an (x, y) point at <span class="hljs-subst">\(x)</span>, <span class="hljs-subst">\(y)</span>&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> movie <span class="hljs-keyword">as</span> <span class="hljs-type">Movie</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;a movie called <span class="hljs-subst">\(movie.name)</span>, dir. <span class="hljs-subst">\(movie.director)</span>&quot;</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> stringConverter <span class="hljs-keyword">as</span> (<span class="hljs-type">String</span>) -&gt; <span class="hljs-type">String</span>:
        <span class="hljs-built_in">print</span>(stringConverter(<span class="hljs-string">&quot;Michael&quot;</span>))
    <span class="hljs-keyword">default</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;something else&quot;</span>)
    }
}

<span class="hljs-comment">// zero as an Int</span>
<span class="hljs-comment">// zero as a Double</span>
<span class="hljs-comment">// an integer value of 42</span>
<span class="hljs-comment">// a positive double value of 3.14159</span>
<span class="hljs-comment">// a string value of &quot;hello&quot;</span>
<span class="hljs-comment">// an (x, y) point at 3.0, 5.0</span>
<span class="hljs-comment">// a movie called Ghostbusters, dir. Ivan Reitman</span>
<span class="hljs-comment">// Hello, Michael</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p><code>Any</code> &#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x5305;&#x62EC;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x3002;Swift &#x4F1A;&#x5728;&#x4F60;&#x7528; <code>Any</code> &#x7C7B;&#x578B;&#x6765;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x7ED9;&#x4F60;&#x4E00;&#x4E2A;&#x8B66;&#x544A;&#x3002;&#x5982;&#x679C;&#x4F60;&#x786E;&#x5B9E;&#x60F3;&#x4F7F;&#x7528; <code>Any</code> &#x7C7B;&#x578B;&#x6765;&#x627F;&#x8F7D;&#x53EF;&#x9009;&#x503C;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>as</code> &#x64CD;&#x4F5C;&#x7B26;&#x663E;&#x5F0F;&#x8F6C;&#x6362;&#x4E3A; <code>Any</code>&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
</blockquote>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> optionalNumber: <span class="hljs-type">Int</span>? = <span class="hljs-number">3</span>
things.append(optionalNumber)        <span class="hljs-comment">// &#x8B66;&#x544A;</span>
things.append(optionalNumber <span class="hljs-keyword">as</span> <span class="hljs-type">Any</span>) <span class="hljs-comment">// &#x6CA1;&#x6709;&#x8B66;&#x544A;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="17_Error_Handling.html" class="navigation navigation-prev " aria-label="Previous page: 错误处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="19_Nested_Types.html" class="navigation navigation-next " aria-label="Next page: 嵌套类型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"类型转换","level":"1.3.18","depth":2,"next":{"title":"嵌套类型","level":"1.3.19","depth":2,"path":"chapter2/19_Nested_Types.md","ref":"chapter2/19_Nested_Types.md","articles":[]},"previous":{"title":"错误处理","level":"1.3.17","depth":2,"path":"chapter2/17_Error_Handling.md","ref":"chapter2/17_Error_Handling.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/18_Type_Casting.md","mtime":"2019-07-05T01:01:08.108Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

